//
//  BaseViewController.m
//  ContinueEDUPhone
//
//  Created by SL on 13-11-25.
//  Copyright (c) 2013年 Sheng Long. All rights reserved.
//

#import "BaseViewController.h"
#import "SettingViewController.h"

#define LOADING_FAILED_TIP_TEXT         @"点击屏幕，重新加载"
#define LOADING_NODATA_TIP_TEXT         @"暂无数据\n点击屏幕，重新加载"

@interface BaseViewController ()
{
    //提示Label
    UILabel *tipLabel;
}
@property (nonatomic,retain) UIViewController *leftVC;
@end

@implementation BaseViewController

@synthesize netArray;
@synthesize leftVC;
@synthesize currentShowLeft;
@synthesize showBackBtn;

-(void)dealloc
{
    SafeRelease(navigationBarView)
    SafeRelease(progressView)
    for (ASIHTTPRequest * req in self.netArray) {
        [req clearDelegatesAndCancel];
    }
    self.netArray = nil;
    SafeRelease(leftVC)
    SafeRelease(tipLabel)
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if (CURRENT_VERSION_SEVEN) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
    
    //背景色
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    //网络请求
    self.netArray = [NSMutableArray arrayWithCapacity:1];
    
    //添加导航条
    navigationBarView = [[NavigatonBarView alloc]initWithFrame:CGRectMake(0, 0,NAVIGATIONBAR_WIDTH,NAVIGATIONBAR_HEIGHT)];
    [navigationBarView setBackgroundColor:GET_COLOR(@"ff8cb7")];
    [navigationBarView setDelegate:self];
    [self.view addSubview:navigationBarView];
}

-(void)viewDidAppear:(BOOL)animated
{
    CLog(@"viewDidAppearviewDidAppear");
    if (currentShowLeft) {
        [self setLeftDrawerViewControllerEnabled:YES];
    }
}

-(void)viewDidDisappear:(BOOL)animated
{
    CLog(@"viewDidDisappearviewDidDisappear");
    if (currentShowLeft) {
        [self setLeftDrawerViewControllerEnabled:NO];
    }
}

-(void)leftButtonClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)rightButtonClick
{
    [self.navigationController pushViewController:[[SettingViewController alloc] init] animated:YES];
}

#pragma mark - ASIHTTPRequestDelegate
//交给父类来完成ASIHTTPRequest请求
-(void)superASIStartAsynchronous:(ASIHTTPRequest*)request
{
    [request setDidFinishSelector:@selector(superRequestFinished:)];
    [request setDidFailSelector:@selector(superRequestFailed:)];
    [request startAsynchronous];
}

-(void)superRequestFinished:(ASIHTTPRequest *)request
{
    CLog(@"superRequestFinished====%@",[request responseString]);
    NSDictionary *dic = [[request responseString] objectFromJSONString];
    if (dic != nil) {
        NSString *text = [[dic valueForKeyPath:@"error"] valueForKey:@"message"];
        if (text.length > 0) {
            if ([text isEqualToString:@"数据不存在"]) {
                text = @"账号不存在";
            }
            [self getASIRecordFailed:request data:text];
        }else{
            [self getASIRecordSucceed:request data:dic];
        }
    }else{
        [self superRequestFailed:request];
    }
}

-(void)superRequestFailed:(ASIHTTPRequest *)request
{
    CLog(@"superRequestFailed====%@",[request responseString]);
    [self requestFailed:request];
}

//父类获取服务器返回信息，成功获取到数据执行方法
-(void)getASIRecordSucceed:(ASIHTTPRequest *)request data:(NSDictionary*)dic{}
//父类获取服务器返回信息，未成功获取到数据执行方法
-(void)getASIRecordFailed:(ASIHTTPRequest *)request data:(NSString*)failed{}

#pragma mark - 侧边栏操作
//显示侧边栏
-(void)setShowLeftDrawerViewController
{
    CLog(@"MMDrawerSideLeft===%d",MMDrawerSideLeft);
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}

//是否启用显示侧边栏
-(void)setLeftDrawerViewControllerEnabled:(BOOL)Enabled
{
    if (Enabled) {
        if (leftVC) {
            [self.mm_drawerController setLeftDrawerViewController:leftVC];  
        }
    }else{
        if (!leftVC) {
            self.leftVC = self.mm_drawerController.leftDrawerViewController;
        }
        [self.mm_drawerController setLeftDrawerViewController:nil];
    }
}

#pragma mark - ProgressView Event 不阻塞提示框
//阻塞当前视图self.view提示框    belowSubview下面
- (void)showProgressView:(NSString *)title belowSubview:(UIView*)belowSubview
{
    [tipLabel removeFromSuperview];
    
    if (!progressView) {
        progressView = [[MBProgressHUD alloc] initWithView:self.view];
        progressView.dimBackground = NO;
    }
    
    //有状态栏放在状态栏下层，没有放在试图顶层
    if (belowSubview) {
        [self.view insertSubview:progressView belowSubview:belowSubview];
    }else{
        [self.view addSubview:progressView];
    }

    if (title)
        progressView.labelText = title;
    [progressView show:YES];
}

//阻塞指定视图currentView提示框
-(void)blockingCurrentView:(UIView*)currentView title:(NSString*)title
{
    [tipLabel removeFromSuperview];
    
    if (!currentView) {
        return;
    }
    
    if (!progressView) {
        progressView = [[MBProgressHUD alloc] initWithView:self.view];
        progressView.dimBackground = NO;
    }
    [currentView setUserInteractionEnabled:NO];
    [currentView addSubview:progressView];
    if (title)
        progressView.labelText = title;
    [progressView show:YES];
}

//消失的提示框
-(void)hideProgressView
{
    [progressView.superview setUserInteractionEnabled:YES];
    [progressView removeFromSuperview];
    [progressView hide:YES];
}

//可点击重新加载view   type==0加载失败     type==1没有数据
-(void)clickAgainLoadingView:(NSInteger)type
{
    [self hideProgressView];
    [LoadingFrameView directlyDisappear];
    
    if (!tipLabel) {
        tipLabel = [[UILabel alloc] initWithFrame:self.view.bounds];
        [tipLabel setBackgroundColor:[UIColor clearColor]];
        [tipLabel setNumberOfLines:0];
        [tipLabel setFont:[UIFont boldSystemFontOfSize:20]];
        [tipLabel setTextColor:[UIColor lightGrayColor]];
        [tipLabel setUserInteractionEnabled:YES];
        [tipLabel setTextAlignment:NSTextAlignmentCenter];
        
        UITapGestureRecognizer *gest = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(againLoading)] autorelease];
        [tipLabel addGestureRecognizer:gest];
    }
    if (type) {
        [tipLabel setText:LOADING_NODATA_TIP_TEXT];
    }else{
        [tipLabel setText:LOADING_FAILED_TIP_TEXT];
    }
    [self.view insertSubview:tipLabel belowSubview:navigationBarView];
}

//重新加载数据
-(void)againLoading
{
    
}

#pragma mark - 本地数据处理
//获取本地数据是否成功，Failed是否获取网络数据失败的时候加载数据
-(BOOL)getLocalData:(BOOL)Failed
{
    return NO;
}

//获取本地数据
-(NSString*)getLocalData
{
    NSString *title = @"";
    if (navigationBarView.titleText) {
        title = navigationBarView.titleText;
    }
    NSString *text = [NSString stringWithFormat:@"%@%@%@",APP_USER_MSG.id,title,NSStringFromClass([self class])];
    return [APP_USERDEFAULTS valueForKey:text];
}

//存储本地数据
-(BOOL)saveLocalData:(NSString*)data
{
    NSString *title = @"";
    if (navigationBarView.titleText) {
        title = navigationBarView.titleText;
    }
    NSString *text = [NSString stringWithFormat:@"%@%@%@",APP_USER_MSG.id,title,NSStringFromClass([self class])];
    [APP_USERDEFAULTS setValue:data forKeyPath:text];
    return YES;
}

#pragma mark - 调用页面显示导航或者返回按钮
//显示navbar样式
-(void)showNavbarStyle
{
    if (showBackBtn) {
        [navigationBarView setNormalLeftButton];
    }else{
        [navigationBarView setNormalLeftMenuButton];
        [navigationBarView setNormalRightButton];
    }
}

#pragma mark - 方向和状态栏控制
- (BOOL)prefersStatusBarHidden
{
    return NO;
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    //状态栏字体颜色
    return UIStatusBarStyleLightContent;
}

//ios6
-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    //这里写你需要的方向
    return UIInterfaceOrientationMaskPortrait;
}

//ios6以下
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIDeviceOrientationPortrait);
}

@end



